package dsk.demo.product.controller.supplier;

import dsk.demo.common.utils.R;
import dsk.demo.product.entity.SupplierLinkManEntity;
import dsk.demo.product.service.SupplierLinkManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
@RequestMapping("supplier/supplierLinkMan")
public class SupplierLinkManController {

    @Autowired
    SupplierLinkManService supplierLinkManService;

    /**
     * 新增
     */
    @RequestMapping("/save")
    public R save(@RequestBody SupplierLinkManEntity entity){
        try {
            supplierLinkManService.save(entity);
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
            return R.error(501,"新增联系人信息失败");
        }
        return R.ok();
    }

    /**
     * 单个查询
     */
    @RequestMapping("/info/{id}")
    public R save(@PathVariable("id") Integer id){
        try {
            SupplierLinkManEntity byId = supplierLinkManService.getById(id);
            return R.ok().put("supplierLinkManInfo",byId);
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
            return R.error(501,"新增联系人信息失败");
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        try {
            supplierLinkManService.removeByIds(Arrays.asList(ids));
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
            return R.error(501,"删除联系人信息失败");
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SupplierLinkManEntity entity){
        try {
            supplierLinkManService.updateById(entity);
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
            return R.error(501,"删除联系人信息失败");
        }
        return R.ok();
    }

}
